package 二叉树练习题;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class O多个子节点的树结构进行平铺层序遍历 {

    private static class MutliNode{
        private Integer Id;//
        private Integer parentId;//树的父节点
        private Integer high;//树的高度
        private List<MutliNode> childNodeList;

        public Integer getId() {
            return Id;
        }

        public void setId(Integer id) {
            Id = id;
        }

        public Integer getParentId() {
            return parentId;
        }

        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }

        public List<MutliNode> getChildNodeList() {
            return childNodeList;
        }

        public void setChildNodeList(List<MutliNode> childNodeList) {
            this.childNodeList = childNodeList;
        }

        public Integer getHigh() {
            return high;
        }

        public void setHigh(Integer high) {
            this.high = high;
        }
    }

    private static Integer pkId=0;

    private static Integer treeHigh=5;




    //public static void main(String[] args) {
    //    MutliNode root=new MutliNode();
    //    root.setId(pkId);
    //    root.setParentId(0);
    //    root.setHigh(1);
    //    List<MutliNode> mutliNodes = genertListNode(root.getId(),root.getHigh());
    //    root.setChildNodeList(mutliNodes);
    //    sequenceOutPut(root);
    //
    //}

    /**
     * 层序遍历MutleNode
     * @param node
     */
    public static void sequenceOutPut(MutliNode node){
        if(node==null){
            System.out.println("节点非法");
        }
        Queue<MutliNode> queue = new LinkedList<>();
        queue.add(node);
        while (!queue.isEmpty()) {
            MutliNode poll = queue.poll();
            System.out.println("当前节点id为：" + poll.getId() + "当前节点的父节点:"+poll.getParentId()+",当前节点所在树的高度为：" + poll.getHigh() + " ");
            if(poll.getChildNodeList()!=null){
                poll.getChildNodeList().stream().forEach(x->queue.add(x));
            }
        }
    }

    public static List<MutliNode> genertListNode(Integer id,Integer high){
        if(high>treeHigh) {
            return null;
        }
        List<MutliNode> list = new ArrayList<>();
        high+=1;
        for (int i = 0; i < 3; i++) {
            MutliNode childNode=new MutliNode();
            childNode.setParentId(id);
            childNode.setId(++pkId);
            childNode.setHigh(high);
            List<MutliNode> list1 = genertListNode(childNode.getId(),childNode.getHigh());
            childNode.setChildNodeList(list1);
            list.add(childNode);
        }
        return list;
    }

}
